using System;
using System.Configuration;
using System.Xml;
using WCSFContrib.PageFlow.Xml.Properties;
using System.Globalization;
using System.Xml.XPath;

namespace WCSFContrib.PageFlow.Xml.Configuration
{
    /// <summary>
    /// Base class for all providers settings within the UIP configuration settings in the configuration file.
    /// </summary>
    public class ObjectTypeSettings
    {
        #region Declares Variables
        private const string AttributeName = "name";
        private const string AttributeType = "type";
        private const string Comma = ",";

        private string _name;
        private Type _type;
        #endregion

        #region Constructors
        /// <summary>
        /// Overloaded. Default constructor.
        /// </summary>
        public ObjectTypeSettings() { }

        /// <summary>
        /// Overloaded. Initialized an instance of the ObjectTypeSettings class using the specified configNode.
        /// </summary>
        /// <param name="configNode">The XmlNode from the configuration file.</param>
        public ObjectTypeSettings(IXPathNavigable configNode)
        {
            LoadAttributes((XmlNode)configNode);
        }

        private void LoadAttributes(XmlNode configNode)
        {
            XmlNode currentAttribute;

            //Gets the typed object attributes
            currentAttribute = configNode.Attributes.RemoveNamedItem(AttributeType);
            string fullType;
            if (currentAttribute.Value.Trim().Length > 0)
                fullType = currentAttribute.Value;
            else
                throw new ConfigurationErrorsException(String.Format(CultureInfo.CurrentCulture, Resources.InvalidXmlAttributeValue, AttributeName, configNode.Name));
            
            //  fix up type/asm strings
            _type = Type.GetType(fullType);
            
            currentAttribute = configNode.Attributes.RemoveNamedItem(AttributeName);
            if (currentAttribute.Value.Trim().Length > 0)
                _name = currentAttribute.Value;
            else
                throw new ConfigurationErrorsException(String.Format(CultureInfo.CurrentCulture, Resources.InvalidXmlAttributeValue, AttributeName, configNode.Name));
        }
        #endregion


        #region Properties
        /// <summary>
        /// Gets the object name.
        /// </summary>
        public String Name
        {
            get { return _name; }
        }

        /// <summary>
        /// Gets the object fully qualified type name.
        /// </summary>
        public Type Type
        {
            get { return _type; }
        }

        #endregion
    }
}
